package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.Notice;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.service.NoticeService;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;
    @Autowired
    private TokenService tokenService;

    /**
     * @description: 获取当前用户的公告列表
     * @author: mo.shanyong
     * @date: 2025/2/28 11:03
     * @param: token
     * @param: accountId 区域id
     * @param: pageNum
     * @param: pageSize
     * @param: request
     * @param: response
     */
    @GetMapping("/getUserNoticeList")
    public void getUserNoticeList(String token,Integer accountId,Integer pageNum,Integer pageSize,HttpServletRequest request,HttpServletResponse response){
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            User user = tokenService.getUser(token);
            log.info("getUserNoticeList------->pageNum:{},pageSize:{},token:{},accountId:{}",pageNum,pageSize,token,accountId);
            List<Notice> notices = noticeService.getUserNoticeList(pageNum, pageSize, user,accountId);
            int noticeCount = noticeService.getUserNoticeCount(user);
            int userNotReadNoticeCount = noticeService.getUserNotReadNoticeCount(user);
            map.put("data",notices);
            map.put("total",noticeCount);
            map.put("num",userNotReadNoticeCount);
            map.put("ret",1);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }
    /**
     * @description: 记录对公告的操作日志
     * @author: mo.shanyong
     * @date: 2025/2/20 15:48
     * @param: operateType 操作类型：3、点击；5、阅读(目前只记了这个)
     * @return: com.tbit.uqbike.object.view.JsonResult
     */
    @PostMapping("/recordNoticeLog")
    public void recordNoticeLog(String token, Integer noticeId , Integer operateType, HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            User user = tokenService.getUser(token);
            log.info("recordNoticeLog------->noticeId:{},operateType:{},token:{}",noticeId,operateType,token);
            Integer logId = noticeService.recordNoticeLog(noticeId, operateType, user);
            map.put("data",logId);
            map.put("ret",1);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }
}
